import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import axios from "axios";

// axios配置项
axios.defaults.baseURL = 'http://127.0.0.1:3000';  // 默认地址
// axios.defaults.baseURL = "http://liangwei.tech:3000";
//请求拦截
axios.interceptors.request.use(req => {
  if (localStorage.getItem("token")) {
    // 有token的话,自动带上Authorization
    req.headers.Authorization = localStorage.getItem("token");
  }
  return req;
});
//响应拦截
axios.interceptors.response.use(req => {
  const { statusCode } = req.data;
  // 检测4开头的错误正则表达式
  const fourErrRegExp = /^4\d\d/;
  if (fourErrRegExp.test(statusCode)) {
    localStorage.removeItem("token");
    localStorage.removeItem("user_id");
    router.push({ path: "/" });
  }
  return req;
});
//全局前置守卫
router.beforeEach((to, from, next) => {
  // 禁止匿名进入的数组
  const guardArr = ["/person",'/personExit','/particulars"',"/attention","/follow","/collect"];
  const token = localStorage.getItem("token");
  if (guardArr.indexOf(to.path) > -1) {
    if (!token) {
      router.push({
        //如果匿名进入其他守卫数组页面 则进入登录页面
        path: "/login"
      });
    }
  }
  next();
});

// 导入vant-ui组件
import Vant from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

Vue.prototype.$axios = axios;
Vue.config.productionTip = false;

new Vue({
  router,
  render: h => h(App)
}).$mount("#app");
